package com.tony.akka.sample1;

import akka.actor.*;
import akka.japi.pf.ReceiveBuilder;
import com.tony.akka.StdIn;

import javax.websocket.OnMessage;

/**
 * @version V1.0  </br>
 * @ClassName App </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 3/19/2019 10:01 AM</br>
 */
public class App {
    static class Counter extends AbstractLoggingActor{
        static class Message{}

        private int counter = 0;

        @Override
        public Receive createReceive() {
            return receiveBuilder().match(Message.class, this::onMessage).build();
        }

        private  void onMessage(Message pMessage) {
            counter++;
            log().info("Increased counter " + counter);
        }

        static Props props() {
            return Props.create(Counter.class);
        }
    }

    public static void main(String[] args){
        ActorSystem system = ActorSystem.create("sample1");
        ActorRef couterActor = system.actorOf(Counter.props(), "couterActor");
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int j = 0; j < 5; j++) {
                    couterActor.tell(new Counter.Message(), ActorRef.noSender());
                }
            }).start();

        }

        System.out.println("ENTER to terminate");
        StdIn.readLine();
    }
}
